import Footer from '@/components/Footer';
import { LockOutlined, UserOutlined, FunctionOutlined } from '@ant-design/icons';
import { LoginForm, ProFormText } from '@ant-design/pro-components';
import { Alert, message, Spin, Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import { FormattedMessage, history, SelectLang, useDispatch } from 'umi';
import styles from './index.less';
import { getCaptcha, login } from '@/service/api/login';
import { Captcha, LoginAndRegister } from '@/types/login';
// 验证
const Login: React.FC = () => {
  const [type, setType] = useState<string>('account');
  const [captchaImg, setCaptchaImg] = useState<Captcha>();
  const dispatch = useDispatch();
  const getCaptchaImage = async () => {
    const res: Captcha = await getCaptcha();
    setCaptchaImg(res);
  };
  useEffect(() => {
    getCaptchaImage();
  }, []);

  return (
    <div className={styles.container}>
      <div className={styles.lang} data-lang>
        {SelectLang && <SelectLang />}
      </div>
      <div className={styles.content}>
        {/* <Spin spinning={spinLoading} delay={500} tip="登录中..." size="large"> */}
        <LoginForm
          contentStyle={{
            minWidth: 280,
            maxWidth: '75vw',
          }}
          logo={<img alt="logo" src="/images/logo.png" />}
          title="探险之旅"
          subTitle={'寻找当地导游，并从最了解他们的人那里预订冒险'}
          onFinish={async (values: LoginAndRegister) => {
            try {
              const res: any = await login({
                ...values,
                uuid: captchaImg?.uuid,
              });
              if (res) {
                message.success({
                  content: res.msg,
                });
                localStorage.setItem('token', res.token);
                dispatch({ type: 'userInfoModel/updateLoginState', isLogin: true });
                history.push('/');
              }
            } catch (err: any) {
              if (err.message === '验证码已失效') {
                getCaptchaImage();
                return;
              }
              if (err.message === '验证码错误') {
                getCaptchaImage();
                return;
              }
            } finally {
            }
          }}
        >
          <Tabs
            activeKey={type}
            onChange={setType}
            centered
            items={[
              {
                key: 'account',
                label: '账户密码登录',
              },
            ]}
          />
          {type === 'account' && (
            <>
              <ProFormText
                name="username"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined />,
                }}
                placeholder={'用户名: admin'}
                rules={[
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="pages.login.username.required"
                        defaultMessage="请输入用户名!"
                      />
                    ),
                  },
                ]}
              />
              <ProFormText.Password
                name="password"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined />,
                }}
                placeholder={'密码: admin123'}
                rules={[
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="pages.login.password.required"
                        defaultMessage="请输入密码！"
                      />
                    ),
                  },
                ]}
              />
              <div className={styles.captchaItem}>
                <ProFormText
                  fieldProps={{
                    size: 'large',
                    prefix: <FunctionOutlined />,
                  }}
                  name="code"
                  placeholder={'验证码'}
                  rules={[
                    {
                      required: true,
                      message: (
                        <FormattedMessage
                          id="pages.login.code.required"
                          defaultMessage="请输入验证码！"
                        />
                      ),
                    },
                  ]}
                />
                <img
                  src={'data:image/gif;base64,' + captchaImg?.img}
                  alt=""
                  onClick={getCaptchaImage}
                />
              </div>
            </>
          )}
          <div
            style={{
              marginBottom: 40,
            }}
          >
            <a
              style={{
                float: 'right',
              }}
              href="/"
            >
              返回首页
            </a>
          </div>
        </LoginForm>
        {/* </Spin> */}
      </div>
      <Footer />
    </div>
  );
};

export default Login;
